package ca.cserkesz.healthform;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker.StateValue;

/**
 *
 * @author Laza Gyuri cst. <laza.gyuri@cserkesz.ca>
 */
public class HealthFormController {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        final HealthFormFrame frame = new HealthFormFrame();
        
        frame.setVisible(true);
        
        frame.addOpenActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                
                final ExcelReader reader = new ExcelReader();
                
                reader.addPropertyChangeListener(new PropertyChangeListener() {

                    @Override
                    public void propertyChange(PropertyChangeEvent pce) {
                        try {
                            if (reader.getState() == StateValue.PENDING) {
                                frame.setParsingProgress(-1);
                            } else if (reader.getState() == StateValue.STARTED) {
                                frame.setParsingProgress(reader.getProgress());
                            } else { // DONE
                                frame.setParsingProgress(100);
                                frame.setScoutList(reader.get());
                            }
                        } catch (Exception ex) {
                            Logger.getLogger(HealthFormController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }

                });
                reader.execute();
            }
        });
        
        
        
        
        frame.addSaveActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                final PDFGenerator generator = new PDFGenerator(frame.getSelectedScouts());
                generator.addPropertyChangeListener(new PropertyChangeListener() {

                        @Override
                        public void propertyChange(PropertyChangeEvent pce) {
                            frame.setSavingProgress(generator.getProgress());
                        }

                });
                generator.setCampName(frame.getCampName());
                generator.execute();
                
            }
        });
    }

}
